﻿@using PagedList.Mvc;

@model PagedList.IPagedList<Order>

@section styles{
    @Styles.Render("~/Content/PagedList.css")
}

@functions {
    private string SortOrderLink(string displayName, string name, string search, int pageSize, bool isTrash)
    {
        string url = Url.Action("Index", new
        {
            sortOrder = ViewData[name + "Sort"],
            search = search,
            pageSize = pageSize,
            isTrash = isTrash
        });

        string icon = String.IsNullOrWhiteSpace(ViewData[name + "Icon"] as String) ? "": ViewData[name + "Icon"] as String;

        return "<a href='" + url + "'>" + displayName + icon + "</a>";
    }
}

@{   
    var strHp = new StringHelper();
     
    string search = ViewBag.Search;
    string sortOrder = ViewBag.SortOrder;
    int pageSize = ViewBag.PageSize;
    bool isTrash = ViewBag.IsTrash;
    int trashCount = ViewBag.TrashCount;
    int listCount = ViewBag.ListCount;
    string returnListUrl = ViewBag.ReturnListUrl;
    SelectList pageSizeDDL = ViewBag.PageSizeDDL;

    float exchangeRate = ViewBag.ExchangeRate;

    int index = Model.FirstItemOnPage;

    ViewBag.Title = "Danh sách đơn hàng";
}

<section class="content-header">
    <h1>
        Đơn hàng <small>@(!isTrash ? "danh sách" : "thùng rác")</small>
    </h1>
    <ol class="breadcrumb">
        <li>
            <a href="@Url.Action("Index", "Admin")">
                <i class="fa fa-home"></i> Trang chủ
            </a>
        </li>
        <li class="active">@(!isTrash ? "Danh sách" : "Thùng rác") Đơn hàng</li>
    </ol>
</section>

<section class="content">
    <div class="box box-primary">
        <div class="box-body">

            <!-- toolbars -->
            @using (Html.BeginForm(null, null, FormMethod.Get))
            {
                @Html.Hidden("IsTrash", isTrash)

                <div class="row">
                    <div class="col-md-7">
                        @Html.DropDownList("PageSize", pageSizeDDL, new
                        {
                            @class = "form-control auto-width inline",
                            onchange = "this.form.submit()"
                        })

                        @if (!isTrash)
                        {
                            <a href="@Url.Action("Index", new { isTrash = true })" class="btn btn-danger @(trashCount > 0 ? "" : "disabled")">
                                <i class="fa fa-trash-o"></i> Thùng rác (@trashCount)
                            </a>
                        }
                        else
                        {
                            <a href="@Url.Action("Index")" class="btn btn-success">
                                <i class="fa fa-file-text"></i> Danh sách (@listCount)
                            </a>
                        }
                    </div>
                    <div class="col-md-5">
                        <div class="input-group">
                            @Html.TextBox("Search", search, new
                            {
                                @class = "form-control",
                                name = "search",
                                placeholder = "Mã đơn hàng"
                            })
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="submit">
                                    <i class="fa fa-search"></i> Tìm kiếm
                                </button>
                            </span>
                        </div>

                    </div>
                </div>
            }

            <br />

            <!-- gridview -->
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>
                                @Html.Raw(SortOrderLink("Mã đơn hàng", "Code", search, pageSize, isTrash))
                            </th>
                            <th>
                                Trị giá
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Tài khoản đặt hàng", "CreatedBy", search, pageSize, isTrash))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Tên người nhận", "Name", search, pageSize, isTrash))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Đặt hàng lúc", "DateCreated", search, pageSize, isTrash))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Tình trạng", "PaymentStatus", search, pageSize, isTrash))
                            </th>
                            <th class="text-center">
                                <a id="btnRefresh" href="@Request.Url.AbsoluteUri" class="btn btn-success btn-sm hidden">
                                    <i class="fa fa-refresh"></i> Làm mới
                                </a>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach(Order item in Model)
                        {
                            float price = item.OrderStyles.Sum(x => x.Quantity * x.Style.UnitPrice);
                            
                            <tr>
                                <td>
                                    @(index++)
                                </td>
                                <td>
                                    @item.Code
                                </td>
                                <td class="text-right">
                                    <div class="text-danger">
                                        @strHp.PriceFormat(price, "usd")
                                    </div>
                                    <div class="text-success">
                                        @strHp.PriceFormat((price * exchangeRate), "vnd")
                                    </div>
                                </td>
                                <td>
                                    @item.CreatedBy
                                </td>
                                <td>
                                    @item.Name
                                </td>
                                <td>
                                    @item.DateCreated
                                </td>
                                <td>
                                    @if (item.PaymentStatus == "0")
                                    {
                                        <span class="text-success">Thành công</span>
                                    }
                                    else
                                    {
                                        <span class="text-danger">Thất bại (@item.PaymentStatus)</span>
                                    }
                                </td>
                                <td class="control">
                                    <a class="no-href" data-id="@item.Id" data-toggle="tooltip" data-placement="top"
                                       title="Xóa tạm thời" onclick="Recycle(this, 'Order', 0)">
                                        <i class="fa fa-trash-o"></i>
                                    </a>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>

            <br />

            <!-- pagelist -->
            <div class="row">
                <div class="col-md-6">
                    <button type="button" class="btn btn-default" disabled="disabled">
                        @Model.FirstItemOnPage - @Model.LastItemOnPage / @Model.TotalItemCount
                    </button>
                </div>
                <div class="col-md-6 text-right">
                    @Html.PagedListPager(Model, page => Url.Action("Index", new
                    {
                        page,
                        sortOrder = sortOrder,
                        search = search,
                        pageSize = pageSize,
                        isTrash = isTrash
                    }), new PagedListRenderOptions
                    {
                        LinkToFirstPageFormat = DataTableConstant.LinkToFirstPageFormat,
                        LinkToPreviousPageFormat = DataTableConstant.LinkToPreviousPageFormat,
                        LinkToNextPageFormat = DataTableConstant.LinkToNextPageFormat,
                        LinkToLastPageFormat = DataTableConstant.LinkToLastPageFormat,
                        MaximumPageNumbersToDisplay = 5
                    })
                </div>
            </div>
        </div>
    </div>
</section>